/**
 * 
 */
package edu.cmu.mism.dgjava.data.ui.composites.option;

import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import edu.cmu.mism.dgjava.data.models.option.ImplicitFiniteDifference;
import edu.cmu.mism.dgjava.data.models.option.impl.OptionModelFactoryImpl;
import edu.cmu.mism.dgjava.utils.IDGConstants;
import edu.cmu.mism.dgjava.utils.SWTUtils;
import edu.cmu.mism.dgjava.views.Messages;

/**
 * @author Ankit
 * 
 */
public class ImplicitFiniteDifferenceComposite extends OptionInputDataComposite {

	private static final String DEFAULT_TIME_TO_EXERCISE = "0.4167";
	private static final String DEFAULT_EX_PRICE = "50.00";
	private static final String DEFAULT_MAX_PRICE = "100.00";
	private static final String DEFAULT_TIME_INTERVALS = "10";
	private static final String DEFAULT_PRICE_INTERVALS = "20";

	private Text _exTimeTxt;
	private Label _exTimeMessageLbl;

	private Text _exPriceTxt;
	private Label _exPriceMessageLbl;

	private Text _timeIntervalTxt;
	private Label _timeIntervalMessageLbl;

	private Text _priceIntervalTxt;
	private Label _priceIntervalMessageLbl;

	private Text _maxPriceTxt;
	private Label _maxPriceMessageLbl;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * edu.cmu.mism.dgjava.data.ui.composites.option.OptionInputDataComposite
	 * #createCustomControls(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	protected void createCustomControls(Composite parent) {

		GridData labelGd = new GridData(GridData.FILL, -1, false, false);
		GridData hGd = new GridData(GridData.FILL, -1, false, false);

		// Exercise Time label
		Label exTimeLbl = SWTUtils.createLabel(parent,
				Messages.OptionDataView_time2ex, 1);

		exTimeLbl.setLayoutData(labelGd);
		// Exercise Time text
		this._exTimeTxt = SWTUtils.createTextBox(parent, 1,
				SWTUtils.DEFAULT_TEXTBOX_WIDTH, -1, true);
		this._exTimeTxt.setText(DEFAULT_TIME_TO_EXERCISE);
		this._exTimeTxt.setLayoutData(hGd);
		this._exTimeMessageLbl = SWTUtils.createLabel(parent, "", 1);
		this._exTimeMessageLbl.setImage(JFaceResources
				.getImage(IDGConstants.DLG_IMG_TITLE_ERROR));

		// Exercise Price Label
		Label exPriceLbl = SWTUtils.createLabel(parent,
				Messages.OptionDataView_exercise_price, 1);
		exPriceLbl.setLayoutData(labelGd);
		// Exercise Price Text
		this._exPriceTxt = SWTUtils.createTextBox(parent, 1,
				SWTUtils.DEFAULT_TEXTBOX_WIDTH, -1, true);
		this._exPriceTxt.setText(DEFAULT_EX_PRICE);
		this._exPriceTxt.setLayoutData(hGd);
		this._exPriceMessageLbl = SWTUtils.createLabel(parent, "", 1);
		this._exPriceMessageLbl.setImage(JFaceResources
				.getImage(IDGConstants.DLG_IMG_TITLE_ERROR));

		// Max Price Label
		Label maxPriceLbl = SWTUtils.createLabel(parent,
				Messages.OptionDataView_maxPriceLabel, 1);
		maxPriceLbl.setLayoutData(labelGd);
		// Max Price Text
		this._maxPriceTxt = SWTUtils.createTextBox(parent, 1,
				SWTUtils.DEFAULT_TEXTBOX_WIDTH, -1, true);
		this._maxPriceTxt.setText(DEFAULT_MAX_PRICE);
		this._maxPriceTxt.setLayoutData(hGd);
		this._maxPriceMessageLbl = SWTUtils.createLabel(parent, "", 1);
		this._maxPriceMessageLbl.setImage(JFaceResources
				.getImage(IDGConstants.DLG_IMG_TITLE_ERROR));

		// Time Intervals Label
		Label timeIntervalLbl = SWTUtils.createLabel(parent,
				Messages.OptionDataView_timeIntervalLabel, 1);
		timeIntervalLbl.setLayoutData(labelGd);
		// Time Intervals Text
		this._timeIntervalTxt = SWTUtils.createTextBox(parent, 1,
				SWTUtils.DEFAULT_TEXTBOX_WIDTH, -1, true);
		this._timeIntervalTxt.setText(DEFAULT_TIME_INTERVALS);
		this._timeIntervalTxt.setLayoutData(hGd);
		this._timeIntervalMessageLbl = SWTUtils.createLabel(parent, "", 1);
		this._timeIntervalMessageLbl.setImage(JFaceResources
				.getImage(IDGConstants.DLG_IMG_TITLE_ERROR));

		// Price Intervals Label
		Label priceIntervalLbl = SWTUtils.createLabel(parent,
				Messages.OptionDataView_priceIntervalLabel, 1);
		priceIntervalLbl.setLayoutData(labelGd);
		// Price Intervals Text
		this._priceIntervalTxt = SWTUtils.createTextBox(parent, 1,
				SWTUtils.DEFAULT_TEXTBOX_WIDTH, -1, true);
		this._priceIntervalTxt.setText(DEFAULT_PRICE_INTERVALS);
		this._priceIntervalTxt.setLayoutData(hGd);
		this._priceIntervalMessageLbl = SWTUtils.createLabel(parent, "", 1);
		this._priceIntervalMessageLbl.setImage(JFaceResources
				.getImage(IDGConstants.DLG_IMG_TITLE_ERROR));
		
		parent.pack();

	}

	@Override
	public Object getDataObject() {

		ImplicitFiniteDifference data = OptionModelFactoryImpl.eINSTANCE
				.createImplicitFiniteDifference();

		data.setExercisePrice(Double.parseDouble(this._exPriceTxt.getText()));
		data.setTimeToExpire(Double.parseDouble(this._exTimeTxt.getText()));
		data.setType(getOptionBaseType());
		data.setMaxPrice(Double.parseDouble(this._maxPriceTxt.getText()));
		data
				.setTimeIntervals(Integer.parseInt(this._timeIntervalTxt
						.getText()));
		data.setPriceIntervals(Integer.parseInt(this._priceIntervalTxt
				.getText()));

		return data;
	}

	/**
	 * Hook up modify listener to observe modification event and validate user's
	 * input
	 */
	protected void hookupModifyListener() {
		this._exPriceTxt.addModifyListener(_validator);
		this._exTimeTxt.addModifyListener(_validator);
		this._maxPriceTxt.addModifyListener(_validator);
		this._priceIntervalTxt.addModifyListener(_validator);
		this._timeIntervalTxt.addModifyListener(_validator);
	}

	@Override
	protected void validate() {
		if (this._exTimeTxt.getText() == null
				|| this._exTimeTxt.getText().length() == 0) {
			this._exTimeMessageLbl
					.setToolTipText("Exercise time can not be empty");
			this._exTimeMessageLbl.setVisible(true);
		} else {
			try {
				Double.valueOf(this._exTimeTxt.getText().trim());
				this._exTimeMessageLbl.setToolTipText(null);
				this._exTimeMessageLbl.setVisible(false);

			} catch (NumberFormatException e) {
				this._exTimeMessageLbl
						.setToolTipText("Exercise time must be a valid decimal number");
				this._exTimeMessageLbl.setVisible(true);
			}
		}

		if (this._exPriceTxt.getText() == null
				|| this._exPriceTxt.getText().length() == 0) {
			this._exPriceMessageLbl
					.setToolTipText("Exercise price can not be empty");
			this._exPriceMessageLbl.setVisible(true);
		} else {
			try {
				Double.valueOf(this._exPriceTxt.getText().trim());
				this._exPriceMessageLbl.setToolTipText(null);
				this._exPriceMessageLbl.setVisible(false);

			} catch (NumberFormatException e) {
				this._exPriceMessageLbl
						.setToolTipText("Exercise price must be a decimal number");
				this._exPriceMessageLbl.setVisible(true);
			}
		}

		if (this._timeIntervalTxt.getText() == null
				|| this._timeIntervalTxt.getText().length() == 0) {
			this._timeIntervalMessageLbl
					.setToolTipText("Time interval must be specified");
			this._timeIntervalMessageLbl.setVisible(true);
		} else {
			try {
				Integer.valueOf(this._timeIntervalTxt.getText().trim());
				this._timeIntervalMessageLbl.setToolTipText(null);
				this._timeIntervalMessageLbl.setVisible(false);

			} catch (NumberFormatException e) {
				this._timeIntervalMessageLbl
						.setToolTipText("Time interval must be a valid decimal number");
				this._timeIntervalMessageLbl.setVisible(true);
			}
		}

		if (this._priceIntervalTxt.getText() == null
				|| this._priceIntervalTxt.getText().length() == 0) {
			this._priceIntervalMessageLbl
					.setToolTipText("Price interval must be specified");
			this._priceIntervalMessageLbl.setVisible(true);
		} else {
			try {
				Integer.valueOf(this._priceIntervalTxt.getText().trim());
				this._priceIntervalMessageLbl.setToolTipText(null);
				this._priceIntervalMessageLbl.setVisible(false);

			} catch (NumberFormatException e) {
				this._priceIntervalMessageLbl
						.setToolTipText("Price interval must be a valid decimal number");
				this._priceIntervalMessageLbl.setVisible(true);
			}
		}

		if (this._maxPriceTxt.getText() == null
				|| this._maxPriceTxt.getText().length() == 0) {
			this._maxPriceMessageLbl
					.setToolTipText("Max price must be specified");
			this._maxPriceMessageLbl.setVisible(true);
		} else {
			try {
				Double.valueOf(this._maxPriceTxt.getText().trim());
				this._maxPriceMessageLbl.setToolTipText(null);
				this._maxPriceMessageLbl.setVisible(false);

			} catch (NumberFormatException e) {
				this._maxPriceMessageLbl
						.setToolTipText("Max price must be a valid decimal number");
				this._maxPriceMessageLbl.setVisible(true);
			}
		}
	}

}
